package demo2.Controller;

import demo2.Mapper.IUserMapper;
import demo2.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import java.util.List;
import java.util.Scanner;

@Controller
public class UserController {
    @Autowired
    private IUserMapper userMapper;
    public void start(){
        Scanner scanner = new Scanner(System.in);

        loop:while (true) {
            System.out.println("请选择功能");
            System.out.println("1.插入用户");
            System.out.println("2.查询用户");
            System.out.println("3.更新用户");
            System.out.println("4.删除用户");
            System.out.println("5.查询所有用户");
            System.out.println("6.退出");
            System.out.println("7.登录");
            System.out.println("8.更新用户2");
            int i = scanner.nextInt();
            switch (i){
                case 1:
                    insert();
                    break;
                case 2:
                    select();
                    break;
                case 3:
                    update();
                    break;
                case 4:
                    delete();
                    break;
                case 5:
                    list();
                    break;
                case 6:
                    System.out.println("退出");
                    break loop;
                case 7:
                    login();
                    break;
                case 8:
                    update2();
                    break;
                default:
                    System.out.println("输入错误");
            }
        }

    }
    private void insert() {
        Scanner scanner = new Scanner(System.in);
        User user = new User();

        // 输入用户名（非空验证）
        while (true) {
            System.out.println("请输入用户名");
            String username = scanner.nextLine().trim();
            if (!username.isEmpty()) {
                user.setUsername(username);
                break;
            }
            System.out.println("用户名不能为空，请重新输入！");
        }

        // 输入密码（非空验证）
        while (true) {
            System.out.println("请输入用户密码");
            String password = scanner.nextLine().trim();
            if (!password.isEmpty()) {
                user.setPassword(password);
                break;
            }
            System.out.println("密码不能为空，请重新输入！");
        }

        // 输入昵称（非空验证）
        while (true) {
            System.out.println("请输入用户昵称");
            String nickname = scanner.nextLine().trim();
            if (!nickname.isEmpty()) {
                user.setNickname(nickname);
                break;
            }
            System.out.println("昵称不能为空，请重新输入！");
        }

        // 输入年龄（非空验证 + 数字验证）
        while (true) {
            System.out.println("请输入用户年龄");
            String ageInput = scanner.nextLine().trim();

            if (ageInput.isEmpty()) {
                System.out.println("年龄不能为空，请重新输入！");
                continue;
            }

            try {
                int age = Integer.parseInt(ageInput);
                if (age > 0 && age < 120) {
                    user.setAge(age);
                    break;
                }
                System.out.println("年龄必须是1-119之间的数字");
            } catch (NumberFormatException e) {
                System.out.println("请输入有效的数字！");
            }
        }

        userMapper.insert(user);
    }
    private User select(){
        Scanner scanner = new Scanner(System.in);
        User user=new User();
        System.out.println("请输入用户名");
        String s = scanner.nextLine();
        User select = userMapper.select(s);
        System.out.println("查询成功");
        System.out.println(select);
        return (select);
    }
    private void update(){
        Scanner scanner = new Scanner(System.in);
        User user=new User();
        System.out.println("请输入用户id");
        user.setId(scanner.nextInt());
        scanner.nextLine();
        System.out.println("请输入用户昵称");
        user.setNickname(scanner.nextLine());
        System.out.println("请输入用户年龄");
        user.setAge(scanner.nextInt());
        userMapper.update(user);
        System.out.println("更新成功");
    }
    private void delete(){
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入用户id");
        int id = scanner.nextInt();
        userMapper.delete(id);
        System.out.println("删除成功");
    }
    private List<User> list(){
        List<User> list = userMapper.list();
        for (User user : list) {
            System.out.println( user);
        }
        return userMapper.list();
    }
    private void login(){
        User user = new User();
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("请输入用户名");
            String username = scanner.nextLine().trim();
            if (!username.isEmpty()) {
                user.setUsername(username);
                break;
            }
            System.out.println("用户名不能为空，请重新输入！");
        }

        // 输入密码（非空验证）
        while (true) {
            System.out.println("请输入用户密码");
            String password = scanner.nextLine().trim();
            if (!password.isEmpty()) {
                user.setPassword(password);
                break;
            }
            System.out.println("密码不能为空，请重新输入！");
        }
        userMapper.login(user);

    }
    private void update2(){
        Scanner scanner = new Scanner(System.in);
        User user = new User();
        while (true) {
            System.out.println("请输入用户名");
            String username = scanner.nextLine().trim();
            if (!username.isEmpty()) {
                user.setUsername(username);
                break;
            }
            System.out.println("用户名不能为空，请重新输入！");
        }
        while (true) {
            System.out.println("请输入用户密码");
            String password = scanner.nextLine().trim();
            if (!password.isEmpty()) {
                user.setPassword(password);
                break;
            }
            System.out.println("密码不能为空，请重新输入！");
        }
        while (true) {
            System.out.println("请输入用户昵称");
            String nickname = scanner.nextLine().trim();
            if(!nickname.isEmpty()){
                user.setNickname(nickname);
                break;
            }
        }
        while (true) {
            System.out.println("请输入用户年龄");
            String ageInput = scanner.nextLine().trim();

            if (ageInput.isEmpty()) {
                System.out.println("年龄不能为空，请重新输入！");
                continue;
            }

            try {
                int age = Integer.parseInt(ageInput);
                if (age > 0 && age < 120) {
                    user.setAge(age);
                    break;
                }
                System.out.println("年龄必须是1-119之间的数字");
            } catch (NumberFormatException e) {
                System.out.println("请输入有效的数字！");
            }
        }
        userMapper.update2(user);
    }

}
